package com.iretailer.report.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;


/**
 * Created by xjw on 2017/8/20.
 * 统一的 http 日志处理
 */
@Aspect
@Component
public class InfluxdbDaoAspect {

    private final static Logger log = LoggerFactory.getLogger(InfluxdbDaoAspect.class);

    @Pointcut("execution(public * com.iretailer.report.influxdb.dao.InfluxdbDao.*(..))")
    public void log() {
    }

    @Around("log()")
    public Object timeArount(ProceedingJoinPoint joinPoint) {

        // 定义返回对象、得到方法需要的参数
        Object obj = null;
        Object[] args = joinPoint.getArgs();
        long startTime = System.currentTimeMillis();
        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            log.error("统计某方法执行耗时环绕通知出错", e);
        }
        // 获取执行的方法名
        long endTime = System.currentTimeMillis();
        String methodName = joinPoint.getSignature().getName();
        log.debug("========  {} time: {}", methodName, (endTime - startTime));

        return obj;
    }

}
